const PAGE_CONF = require('../configs/page'),
      navData = require('../configs/nav'),
      linkData = require('../configs/link'),
      manualData = require('../configs/manual'),
      { infomation } = require('../configs/qr'),
      { IMG_BASE_URL } = require('../configs/url'),
      { searchData } = require('../libs/utils');

const { getSliderData } = require('../services/Slider'),
      { getRecomCourseData } = require('../services/RecomCourse'),
      { getCollectionData } = require('../services/Collection'),
      { getStarTeacherData } = require('../services/Teacher'),
      { getGoodStudentData } = require('../services/Student'),
      { getCourseCategory } = require('../services/CourseTab'),
      { getCourseData } = require('../services//Course');

class Home {
    async index (ctx, next) {

        const sliderData = await getSliderData(),
              recomCourseData = await getRecomCourseData(),
              collectionData = await Promise.all((await getCollectionData()).map(async (item) => item)),
              //  collectionData = await Promise.all(await getCollectionData());//和上一句最终得到的结果是一样的，不明白区别在哪
              starTeacherData = await getStarTeacherData(),
              goodStudentData = await getGoodStudentData();

        await ctx.render('index.ejs', {// .ejs这个尾椎可以不写，会自动去匹配的
            PAGE_CONF: PAGE_CONF.INDEX,
            IMG_BASE_URL,
            navData,
            sliderData,
            recomCourseData,
            collectionData,
            starTeacherData,
            goodStudentData,
            linkData,
            manualData,
            qrInfomation: infomation
        });
    }

    async list (ctx, next) {

        const keyword = ctx.params.kw,
              courseTabData = await getCourseCategory(),
              courseData = await getCourseData();

        await ctx.render('list', {
            PAGE_CONF: PAGE_CONF.LIST,
            IMG_BASE_URL,
            qrInfomation: infomation,
            navData,
            linkData,
            manualData,
            courseTabData,
            courseData: keyword ? searchData(courseData, keyword) : courseData,
            courseDataStr: JSON.stringify(courseData)
        });
    }

    async error (ctx, next) {
        await ctx.render('error', {
            PAGE_CONF: PAGE_CONF.ERROR,
            qrInfomation: infomation,
            navData,
            linkData,
            manualData
        });
    }

}

module.exports = new Home();